package cn.acewill.yxd.yunxundian.adapter;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.TextView;

import org.xutils.DbManager;
import org.xutils.x;

import java.util.HashMap;
import java.util.List;

import cn.acewill.yxd.yunxundian.R;
import cn.acewill.yxd.yunxundian.YXDAPP;
import cn.acewill.yxd.yunxundian.model.reportTailBean;

/**
 * Created by XXX on 2017/9/26.
 */

public class TailAdapter extends BaseAdapter {
    protected YXDAPP yxdapp=new YXDAPP();
    protected DbManager db = x.getDb(yxdapp.getDaoConfig());
    private Context context;
    private List<reportTailBean> tailList;
    public static HashMap<Integer, String> hashMap = new HashMap<Integer, String>();//存放EditText内容的map
    public TailAdapter(Context context, List<reportTailBean> tailList){
        this.context=context;
        this.tailList=tailList;
        hashMap.clear();
    }
    @Override
    public int getCount() {
        return tailList==null?0:tailList.size();
    }

    @Override
    public Object getItem(int position) {
        return tailList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }


    int mFocusPosition = -1;

    View.OnFocusChangeListener mListener = new View.OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            int position = (Integer) v.getTag();
            if (hasFocus) {
                mFocusPosition = position;
            }
        }
    };
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder holder;
        if (convertView==null){
            convertView= LayoutInflater.from(context).inflate(R.layout.head_tail_view,parent,false);
            holder=new ViewHolder();
            holder.itemName = (TextView) convertView.findViewById(R.id.itemName);
            holder.itemValue = (EditText) convertView.findViewById(R.id.itemValue);
            holder.itemValue.setOnFocusChangeListener(mListener);
            holder.itemName.setTag(position);
            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }
        reportTailBean bean = tailList.get(position);
        holder.itemName.setText(bean.getTailName());


        holder.itemValue.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                int position = (int) holder.itemValue.getTag();
                hashMap.put(position,s.toString());
            }
        });

        if (hashMap.get(position)!=null){
            holder.itemValue.setText(hashMap.get(position));
            holder.itemValue.setSelection(hashMap.get(position).length());
        }
        return convertView;
    }
    class ViewHolder{
        TextView itemName;
        EditText itemValue;
    }
}
